package com.vincent.guava.basics;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

import org.testng.annotations.Test;

import com.google.common.base.CharMatcher;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;

public class JoinerSplitterTester {

	@Test
	public void joiner() {
		String[] fantasyGenres = { "Nick", null, "Vince", "Davi", "Cyro" };
		String joined = Joiner.on("-").skipNulls().join(fantasyGenres);
		System.out.println("----------------------------------");
		System.out.println(Arrays.toString(fantasyGenres));
		System.out.println(joined);
	}

	@Test
	public void joinerMap() {
		Map<Integer, String> map = Maps.newHashMap();
		map.put(1, "Nick");
		map.put(2, "Vince");
		map.put(3, "Davi");
		map.put(4, "Cyro");
		String joined = Joiner.on(", ").withKeyValueSeparator(" -> ").join(map);
		System.out.println();
		System.out.println(Objects.toString(map));
		System.out.println(joined);
	}

	@Test
	public void splitter() {
		String input = "Nick-Vince-Davi-Cyro";
		Iterable<String> splitted = Splitter.on("-").split(input);
		System.out.println();
		System.out.println(input);
		System.out.println(Iterables.toString(splitted));
	}

	@Test
	public void splitterUsingCharMatcher() {
		String input = "Some dates like 2012/08/10, 2012/08/10 and 2012/08/10";
		Iterable<String> splitted = Splitter.on(CharMatcher.DIGIT.or(CharMatcher.is('/')).negate())
				.trimResults().omitEmptyStrings().split(input);
		System.out.println();
		System.out.println(input);
		System.out.println(Iterables.toString(splitted));
		System.out.println("----------------------------------");
	}

}
